#include "sdk.h"

ProjectBuildTarget::ProjectBuildTarget( M_Code_Project* parentProject )
  : m_Project( parentProject ), m_FileArray( ProjectFile::CompareProjectFiles ) {
  m_BuildWithAll = false;
  m_CreateStaticLib = true;
  m_CreateDefFile = true;
  m_UseConsoleRunner = true;
  m_FileArray.Clear();
}

ProjectBuildTarget::~ProjectBuildTarget() {
}

M_Code_Project* ProjectBuildTarget::GetParentProject() {
  return m_Project;
}

wxString ProjectBuildTarget::GetFullTitle() const {
  return m_Project->GetTitle() + _T( " - " ) + GetTitle();
}

const wxString & ProjectBuildTarget::GetExternalDeps() const {
  return m_ExternalDeps;
}

void ProjectBuildTarget::SetExternalDeps( const wxString& deps ) {
  if( m_ExternalDeps != deps ) {
    m_ExternalDeps = deps;
    SetModified( true );
  }
}

const wxString & ProjectBuildTarget::GetAdditionalOutputFiles() const {
  return m_AdditionalOutputFiles;
}

void ProjectBuildTarget::SetAdditionalOutputFiles( const wxString& files ) {
  if( m_AdditionalOutputFiles != files ) {
    m_AdditionalOutputFiles = files;
    SetModified( true );
  }
}

bool ProjectBuildTarget::GetIncludeInTargetAll() const {
  return m_BuildWithAll;
}

void ProjectBuildTarget::SetIncludeInTargetAll( bool buildIt ) {
  if( m_BuildWithAll != buildIt ) {
    m_BuildWithAll = buildIt;
    SetModified( true );
  }
}

bool ProjectBuildTarget::GetCreateDefFile() const {
  return m_CreateDefFile;
}

void ProjectBuildTarget::SetCreateDefFile( bool createIt ) {
  if( m_CreateDefFile != createIt ) {
    m_CreateDefFile = createIt;
    SetModified( true );
  }
}

bool ProjectBuildTarget::GetCreateStaticLib() {
  return m_CreateStaticLib;
}

void ProjectBuildTarget::SetCreateStaticLib( bool createIt ) {
  if( m_CreateStaticLib != createIt ) {
    m_CreateStaticLib = createIt;
    SetModified( true );
  }
}

bool ProjectBuildTarget::GetUseConsoleRunner() const {
  if( GetTargetType() == ttConsoleOnly || GetRunHostApplicationInTerminal() ) {
    return m_UseConsoleRunner;
  }
  return false;
}

void ProjectBuildTarget::SetUseConsoleRunner( bool useIt ) {
  if( GetTargetType() == ttConsoleOnly && useIt != m_UseConsoleRunner ) {
    m_UseConsoleRunner = useIt;
    SetModified( true );
  }
}

void ProjectBuildTarget::SetTargetType( TargetType pt ) {
  TargetType ttold = GetTargetType();
  CompileTargetBase::SetTargetType( pt );
  if( ttold != GetTargetType() && GetTargetType() == ttConsoleOnly ) {
    SetUseConsoleRunner( true );
  }
}

void ProjectBuildTarget::AddTargetDep( ProjectBuildTarget* target ) {
  m_TargetDeps.Add( target );
}

BuildTargets& ProjectBuildTarget::GetTargetDeps() {
  return m_TargetDeps;
}

ProjectFile* ProjectBuildTarget::GetFile( int index ) {
  if( m_FileArray.GetCount() == 0 ) {
    for( FilesList::iterator it = m_Files.begin(); it != m_Files.end(); ++it ) {
      if( !*it ) {
        continue;
      }
      m_FileArray.Add( *it );
    }
  }
  if( index < 0 || static_cast<size_t>( index ) >= m_FileArray.GetCount() ) {
    return NULL;
  }
  return m_FileArray.Item( index );
}

bool ProjectBuildTarget::RemoveFile( ProjectFile* pf ) {
  if( !pf ) {
    return false;
  }
  m_Files.erase( pf );
  if( m_FileArray.GetCount() > 0 ) {
    m_FileArray.Remove( pf );
  }
  return true;
}
